package com.dule.Medic.pages;


import org.apache.tapestry5.annotations.CleanupRender;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.dule.Medic.beans.PatientBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.Patient;
import com.dule.Medic.service.PatientService;

public class AddPatient 
{
	
	
	@SuppressWarnings("unused")
	@SessionState      
	@Property      
	private Doctor doctor;     
	private boolean doctorExists;    
	
	@Inject       
	private PatientService patientservice;    
	
	@Persist("flash")  
	@Property
	private Patient patient;
	
	@SuppressWarnings("unused")
	@Property
	@Persist
	private String message; 
	
	public Object onActivate()
	{           
		if(doctorExists)
		{                
			return null;      
		}     
		return FirstPage.class;          
	}   
	
	@SetupRender   
	public void setUp()
	{        
		patient = new PatientBean();    
	}   
	
	@OnEvent(component="patientform")  
	public Object addPatient()
	{    
		Patient addedPatient = patientservice.retrieveOne(patient);   
		if(addedPatient == null)
		{           
			patientservice.save(patient);       
			return Index.class;
		}  
		else
		{
			message = "this patient has been allredy added"; 
			return this;
		}
		         
	} 
	
	@CleanupRender     
	public void cleanUp()
	{             
		message =null;        
	}  
}
